package hlhj.fhp.supreme.activitys

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.Gravity
import android.widget.Toast
import cn.jzvd.JZVideoPlayer
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.onekeyshare.OnekeyShare
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.ClassesPop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.ReadDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.activity_red_detail_aty.*
import kotlinx.android.synthetic.main.option_video.view.*
import java.util.ArrayList
import java.util.HashMap

class RedDetailAty : BaseAty(), ClassesPop.OnClassClickListener {
    private lateinit var classPop:ClassesPop
    private lateinit var dataBean:ReadDetailBean.DataBean
    private var bookId=0
    private var moeny=""
    override fun getContentId(): Int {
        return R.layout.activity_red_detail_aty
    }

    override fun initView() {
        bookId=intent.getIntExtra("id",0)
        moeny=intent.getStringExtra("money")
        videoPlayer.setUp("", JZVideoPlayer.SCREEN_WINDOW_NORMAL,"")
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        OkGo.post<ReadDetailBean>(Urls.BaseUrl+Urls.READDETAIL)
                .params("id",bookId)
                .execute(object :JsonCallBack<ReadDetailBean>(this){
                    override fun onSuccess(response: Response<ReadDetailBean>?) {
                        val body = response?.body()
                        if (body?.code==1){
                            /*观看视频*/
                            tvInfo.text=body?.data.desc
                            if (body?.data!=null){
                                dataBean=body.data
                                val list = ArrayList<ReadDetailBean.DataBean.LessonBean>()
                                list.addAll(body?.data.lesson)
                                tvTotal.text="查看目录：共${body?.data.lesson.size}课时"
                                tvTittle.text=body?.data.title
                                if (list.size>0){
                                    classPop= ClassesPop(this@RedDetailAty,list,this@RedDetailAty)
                                    videoPlayer.setUp(list[0].src,JZVideoPlayer.SCREEN_WINDOW_NORMAL,"第${list[0].sort}课")
                                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.SAVEREAD)
                                            .params("rid",bookId)
                                            .params("lid",list[0].id)
                                            .params("sort",list[0].sort)
                                            .execute(object :JsonCallBack<BaseBean>(this@RedDetailAty){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val bean = response?.body()
                                                    if (bean?.code==1){

                                                    }else{

                                                    }
                                                }
                                            })
                                }else{
                                    classPop= ClassesPop(this@RedDetailAty,list,this@RedDetailAty)
                                }


                            }
                        }else if (body?.code==400){
                            classPop=ClassesPop(this@RedDetailAty,null,this@RedDetailAty)
                            AlertDialog.Builder(this@RedDetailAty).setTitle("您还没有购买该权限，请选择直接购买这本书或是开通会员")
                                    .setPositiveButton("开通会员"){
                                        dialog, which ->
                                        val intent = Intent(this@RedDetailAty, MyVipAty::class.java)
                                        startActivity(intent)
                                        dialog.dismiss()
                                        finish()
                                    }
                                    .setNeutralButton("购买书籍"){
                                        dialog, which ->
                                        val intent = Intent(this@RedDetailAty, PayAty::class.java)
                                        intent.putExtra("id",bookId)
                                        intent.putExtra("money",moeny)
                                        intent.putExtra("et",1)
                                        intent.putExtra("type","2")

                                        startActivity(intent)
                                        dialog.dismiss()
                                        finish()
                                    }
                                    .setNegativeButton("取消"){
                                        dialog, which ->
                                        dialog.dismiss()
                                        finish()
                                    }
                                    .setCancelable(false)
                                    .show()
                        }
                        else{
                            classPop=ClassesPop(this@RedDetailAty,null,this@RedDetailAty)
                        }
                    }
                })

    }

    override fun onPause() {
        super.onPause()
        JZVideoPlayer.releaseAllVideos()
        JZVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR
        JZVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }
    override fun initListener() {
        btShare.setOnClickListener {
            val listener=object :PlatformActionListener{
                override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.SHARE)
                            .execute(object :JsonCallBack<BaseBean>(this@RedDetailAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        ToastUtils.toast(this@RedDetailAty,"分享成功")
                                    }else{
                                        ToastUtils.toast(this@RedDetailAty,bean?.msg!!)
                                    }
                                }
                            })
                }

                override fun onCancel(p0: Platform?, p1: Int) {
                    ToastUtils.toast(this@RedDetailAty,"分享已取消")
                }

                override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                    ToastUtils.toast(this@RedDetailAty,"分享失败")
                }
            }
            val oks = OnekeyShare()
            oks.callback=listener
            oks.setTitle("志尚课堂")
            // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
            oks.setTitleUrl("https://www.baidu.com/")
            // text是分享文本，所有平台都需要这个字段
            oks.text =tvTittle.text.toString()
            //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
            oks.setImageUrl("http://zhishang.hlhjapp.com/static/images/logo.png")
            // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
            //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
            // url仅在微信（包括好友和朋友圈）中使用
            oks.setUrl("https://www.baidu.com/")
            // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            oks.show(this)
        }
        btExit.setOnClickListener {
            finish()
        }
        loClass.setOnClickListener {
            if (classPop.isShowing){
                img.setImageResource(R.mipmap.ic_dushu_gd)
                classPop.dismiss()
            }else{
                img.setImageResource(R.mipmap.ic_dushu_gds)
                if (Build.VERSION.SDK_INT<24){
                    classPop.showAsDropDown(loClass,0,0)
                }else{
                    val location = IntArray(2)
                    loClass.getLocationOnScreen(location)
                    val x =location[0]
                    val y =location[1]
                    classPop.showAtLocation(loClass,Gravity.NO_GRAVITY,0,y+loClass.height)
                }

            }
        }
    }

    override fun onClickClass(i: Int) {
        JZVideoPlayer.releaseAllVideos()
        videoPlayer.setUp(dataBean.lesson[i].src,JZVideoPlayer.SCREEN_WINDOW_NORMAL,"第${dataBean.lesson[i].sort}课")
    }
}
